package com.qf.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qf.entity.Elefee;
import com.qf.entity.Owner;
import com.qf.entity.Parking;
import com.qf.entity.Waterfee;
import com.qf.enums.AppHttpCodeEnum;
import com.qf.mapper.OwnerMapper;
import com.qf.mapper.WaterfeeMapper;
import com.qf.result.ResponseResult;
import com.qf.service.WaterfeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * (Waterfee)表服务实现类
 *
 * @author makejava
 * @since 2023-02-04 22:33:22
 */
@Service("waterfeeService")
public class WaterfeeServiceImpl extends ServiceImpl<WaterfeeMapper, Waterfee> implements WaterfeeService {

    @Resource
    WaterfeeMapper waterfeeMapper;

    @Resource
    OwnerMapper ownerMapper;

    @Override
    public ResponseResult findAll(String ownerName) {

        List<Waterfee> waterfees =new ArrayList<>();

        if (ownerName==null||"".equals(ownerName)) {
            waterfees = waterfeeMapper.selectList(null);
            for (Waterfee waterfee : waterfees){
                Integer ownId = waterfee.getOwnId();
                QueryWrapper queryWrapper1 = new QueryWrapper();
                queryWrapper1.eq("id", ownId);
                Owner owner = ownerMapper.selectOne(queryWrapper1);
                waterfee.setOwnerName(owner.getOwnName());
            }
        }else {
            QueryWrapper<Owner> queryWrapper = new QueryWrapper();
            queryWrapper.like("own_name", ownerName);
            List<Owner> list = ownerMapper.selectList(queryWrapper);
            for (Owner owner : list) {
                Integer oid = owner.getId();
                QueryWrapper queryWrapper1 = new QueryWrapper();
                queryWrapper1.eq("own_id", oid);
                Waterfee waterfee = waterfeeMapper.selectOne(queryWrapper1);
                if (waterfee==null){
                    continue;
                }
                waterfee.setOwnerName(owner.getOwnName());
                waterfees.add(waterfee);
            }

            QueryWrapper<Waterfee> queryWrapper2 = new QueryWrapper();
            queryWrapper2.eq("fee_status","未缴费".contains(ownerName)?0:("已缴费".contains(ownerName)?1:""))
                    .or()
                    .like("house_id",ownerName);
            List<Waterfee> waterfees1 = waterfeeMapper.selectList(queryWrapper2);
            waterfees.addAll(waterfees1);
        }

        return ResponseResult.okResult(waterfees);
    }

    @Override
    public ResponseResult delete(int id) {
        int i = waterfeeMapper.deleteById(id);
        if (i==1) {
            return ResponseResult.okResult();
        }
        return ResponseResult.errorResult(AppHttpCodeEnum.SYSTEM_ERROR);
    }

    @Override
    public ResponseResult add(Waterfee waterfee) {
        int i = waterfeeMapper.insert(waterfee);
        if (i==1) {
            return ResponseResult.okResult();
        }
        return ResponseResult.errorResult(AppHttpCodeEnum.SYSTEM_ERROR);
    }
}
